﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;
using System.Windows.Navigation;
using System.Xml.Serialization;
using NASA.BeAMartian.Controls.Animations;
using System.Xml;
using System.IO;
using NASA.BeAMartian.Controls;

namespace NASA.BeAMartian.Views
{
    public partial class AnimationList : PhoneApplicationPage
    {
        public AnimationList()
        {
            InitializeComponent();

            lbMER.Tag = new LoadMedia("MER");
            lbMSL.Tag = new LoadMedia("MSL");
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            pvt.SelectedIndex = int.Parse(this.NavigationContext.QueryString["x"]);
            base.OnNavigatedTo(e);
        }

        private void pvt_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string galleryName = (e.AddedItems[0] as PivotItem).Tag.ToString();
            string url = String.Format("http://beamdownload.blob.core.windows.net/media/WP7/animations/{0}.xml", galleryName);
            WebClient xmlClient = new WebClient();
            xmlClient.Headers["galleryName"] = galleryName;
            xmlClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(xmlClient_DownloadStringCompleted);
            xmlClient.DownloadStringAsync(new Uri(url, UriKind.Absolute));
        }

        void xmlClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                string uri = ((System.Net.WebClient)(sender)).BaseAddress.ToString();

                XmlSerializer x = new XmlSerializer(typeof(AnimationItems));

                using (XmlReader reader = XmlReader.Create(new StringReader(e.Result)))
                {
                    string galleryName = (sender as WebClient).Headers["galleryName"];
                    AnimationItems ig = (AnimationItems)x.Deserialize(reader);
                    ListBox lb = FindName(String.Format("lb{0}", galleryName)) as ListBox;
                    (lb.Tag as LoadMedia).TotalItems = ig.Count;
                    lb.ItemsSource = ig;
                }
            }
        }

        private void Image_ImageOpened(object sender, RoutedEventArgs e)
        {
            Image img = sender as Image;
            ListBox lb = Utils.VisualTreeUtils.FindParentByType<ListBox>(img);
            LoadMedia lm = lb.Tag as LoadMedia;

            lm.Increment();

            if (lm.IsLoaded)
            {
                ProgressBar pb = FindName(String.Format("pb{0}", img.Tag.ToString())) as ProgressBar;
                pb.IsIndeterminate = false;
                pb.Visibility = System.Windows.Visibility.Collapsed;
                (Resources[String.Format("sbFadeIn{0}", img.Tag.ToString())] as Storyboard).Begin();
            }
        }

        private void ImageButton_Click(object sender, RoutedEventArgs e)
        {
            ImageButton ib = sender as ImageButton;
            MediaPlayerLauncher mediaPlayerLauncher = new MediaPlayerLauncher()
            {
                Media = new Uri(ib.Tag.ToString(), UriKind.Absolute)
            };

            mediaPlayerLauncher.Show();
        }
    }
}
